<?php

namespace app\api\controller;

use app\admin\model\Users;
use app\ApiBaseController;
use think\facade\Cache;

class Login extends ApiBaseController
{
    public function index()
    {
        $post = $this->request->post();
        $rule = [
            'phone|手机号'     =>  'require|mobile',
            'password|密码'    =>  'require',
        ];
        $this->apiValidate($post, $rule);
        $user = Users::where(['phone' => $post['phone']])->find();
        if (empty($user)) {
            return result(1, '手机号不存在');
        }
        if (password($post['password']) != $user->password) {
            return result(1, '密码不正确');
        }
        $hash = md5($user->uid.$user->password);
        $token = signToken($user->uid, $hash);
        return result(0, '登录成功', ['token' => $token]);
    }

}